// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    //弹出层是否显示
    show: false,
    scrollTop: 0,
    active: 0,
    // 用户频道列表
    channelsList:[],
    //第一个的时间戳
    timestampOne:'',
    results:[],
    //上一个时间戳
    pre_timestamp:'',
    //ID
    ID:'',
    //------------弹层--------------------------
    isShow:false,
    isActive:false,
    isActiveId:0,
    optionalList:[],//可选频道
    allChannels:[],//全部频道
    //-----------------------------------------
    artId:"",//文章id
  },
async onLoad(){
const res=await wx.http.get('/v1_0/user/channels')
if (res.message!=='OK') return 
// console.log(res);
//时间戳
const timestamp = Date.now();
// console.log('时间戳',timestamp);
this.setData({
  channelsList:res.data.channels,
  timestampOne:timestamp
})

    //首屏渲染
    const res1=await wx.http.get('/v1_0/articles',{
      channel_id:this.data.channelsList[0].id,
      timestamp:this.data.timestampOne
    })
    // console.log(res1.data.pre_timestamp);
this.setData({
  results:res1.data.results,
  pre_timestamp:res1.data.pre_timestamp
})
// console.log(this.data.results,"首屏");
const {data}= await wx.http.get('/v1_0/channels')

this.setData({
  allChannels:data.channels
})
this.getOptionalList()
// 弹出层的active
const list=this.data.channelsList.map((item)=>{
item.isActive=false
return item
})
list[0].isActive=true
this.setData({
  channelsList:list
})
},
//点击 让盒子动态拥有类
async handleBoxTap(ev){
  // console.log('fu');
// console.log(ev.mark.i);
const {channelsList} =this.data
// 清除所有的动态类
channelsList.forEach((item)=>{
  item.isActive=false
  })
  // 点击让当前盒子动态拥有类
channelsList[ev.mark.i].isActive=true
this.setData({
  channelsList,
  isActiveId:ev.mark.i
})
// 跳转关闭弹出层到页面
this.setData({
  show:false,
  ID:this.data.channelsList[ev.mark.i].id
})
const timestamp = Date.now()
const res1=await wx.http.get('/v1_0/articles',{
  channel_id:this.data.ID,
  timestamp:timestamp,
})
// console.log(res1.data.pre_timestamp);
this.setData({
results:res1.data.results,
pre_timestamp:res1.data.pre_timestamp,
active:ev.mark.i,
isShow:false
})
},
// 获取可选频道列表
async getOptionalList(){
  // console.log(this.allChannels);
// 筛选出可选频道列表
const optionalList=this.data.allChannels.filter((item)=>{
  // console.log(item);
  return !this.data.channelsList.some((i)=>{
    return i.id===item.id
  })
})
// console.log(res);
this.setData({
  optionalList
})
},
async  onChange(event) {
    console.log(event.detail.index,'下标');
    this.setData({
      ID:this.data.channelsList[event.detail.index].id,
    })
    const timestamp = Date.now()
    const res1=await wx.http.get('/v1_0/articles',{
      channel_id:this.data.ID,
      timestamp:timestamp,
      
    })
    // console.log(res1.data.pre_timestamp);
   this.setData({
  results:res1.data.results,
  pre_timestamp:res1.data.pre_timestamp
  })
    // console.log("id",this.data.ID);
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  //scroll-view 组件 bind:scrolltolower 滚动到底部时触发
  async dibu(){
    // console.log('到底部了');
    const res1=await wx.http.get('/v1_0/articles',{
      channel_id:this.data.channelsList[0].id,
      timestamp:this.data.pre_timestamp
    })
    // console.log(res1.data.results);
    // console.log(res1.data.pre_timestamp);
    const {results}=this.data
  results.push(...(res1.data.results))
  this.setData({
    results,
    pre_timestamp:res1.data.pre_timestamp
  })
  },
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false,active:this.data.isActiveId,isShow:false });

  },
//点击显示的弹出层的编辑按钮
bianji(){
  this.setData({
    isShow:!this.data.isShow
  })
},
//点击可选频道里面的每一项
async chilkOptionalList(ev){
  // console.log(ev.mark.id);
  const res=this.data.optionalList.filter((item)=>{
return item.id===ev.mark.id
  })

  this.data.channelsList.push(...res)

  const channels=this.data.channelsList.map((item,index)=>{
    item.seq = index +1
    return item
  })
 const res1=await wx.http.put('/v1_0/user/channels',{channels})
  this.setData({
    channelsList:channels
  })
  this.getOptionalList()
},
//删除我的频道
async delMyChannel(ev){
console.log(this.data.channelsList[this.data.isActiveId].id,"isActiveId");
console.log(ev.mark.id,"ev.mark.id");
// 1.删除我的频道item
const delMyChannel=await wx.http.delete(`/v1_0/user/channels/${ev.mark.id}`)
// console.log(delMyChannel);
//2.获取用户拥有的所有频道
const {data:{channels}}=await wx.http.get('/v1_0/user/channels')
// console.log(channels);
if (this.data.channelsList[this.data.isActiveId].id==ev.mark.id) {
  
  this.setData({
    isActiveId:this.data.isActiveId-1
  })
}
console.log(this.data.isActiveId);
channels[this.data.isActiveId].isActive=true
this.setData({
  channelsList:channels
})
//3.获取剩余的频道
this.getOptionalList()
},
//点击文章跳转到详情页
getDetail(ev){
console.log(ev.mark.artId);
wx.navigateTo({
  url: '/pages/detail/index?id='+ev.mark.artId,
})
}
});